const TokenKey = 'Admin-Token'

export function getToken() {
  let tokenStr = localStorage.getItem(TokenKey)
  if (tokenStr) {
    let token = JSON.parse(tokenStr)
    if (token.expire > new Date().getTime()) {
      return token.token
    } else {
        removeToken()
    }
  }
  return null
}

export function setToken(token) {
  let tokenStr = JSON.stringify({
    token: token,
    expire: new Date().getTime() + 1000 * 60 * 60 * 4
  })
  return localStorage.setItem(TokenKey, tokenStr)
}

export function removeToken() {
  return localStorage.removeItem(TokenKey)
}
